package com.mofang.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import com.mofang.R;
import com.pactera.framework.util.Loger;

public class ExternalDBManager {
	private final int BUFFER_SIZE = 400000;
	public static final String DB_NAME = "city.db"; // 保存的数据库文件名
	private static  String package_name = "";
	public static  String db_path =""; // 在手机里存放数据库的位置

	private static SQLiteDatabase database;
	private Context context;

	public ExternalDBManager(Context context) {
		this.context = context;
		package_name=context.getPackageName();
		db_path="/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ package_name+"/"+"db";
		File dbDir=new File(db_path);
		if(!dbDir.exists()){
			dbDir.mkdirs();
		}
	}

	public SQLiteDatabase openDatabase() {
		this.database = this.openDatabase(db_path + "/" + DB_NAME);
		return database;
	}

	private SQLiteDatabase openDatabase(String dbfile) {
        try {
        	Loger.d(dbfile);
            if (!(new File(dbfile).exists())) {//判断数据库文件是否存在，若不存在则执行导入，否则直接打开数据库
                InputStream is = this.context.getResources().openRawResource(
                        R.raw.city); //欲导入的数据库
                FileOutputStream fos = new FileOutputStream(dbfile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();
            }
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
                    null);
            return db;
        } catch (FileNotFoundException e) {
            Log.e("Database", "File not found");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("Database", "IO exception");
            e.printStackTrace();
        }
        return null;
    }
	public void closeDatabase() {
        this.database.close();
    }
}
